package com.example.lanou.mynews.thread;

import android.os.Handler;
import android.os.Message;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.lanou.mynews.beans.AllDataBeans;
import com.example.lanou.mynews.beans.AllTitles;
import com.example.lanou.mynews.single.SingleMode;
import com.google.gson.Gson;

/**
 * Created by dllo on 16/1/18.
 */
public class AllTitleData extends Thread {
    private Handler handler;
    private final String titleUrl = "http://c.m.163.com/nc/topicset/ios/subscribe/manage/listspecial.html";
    private final String topicUrl="http://c.3g.163.com/nc/article/headline/T1348647909107/0-10.html";
    private AllTitles titles;
    public AllTitleData(Handler handler) {
        this.handler= handler;
    }

    @Override
    public void run() {
        super.run();

        StringRequest titleRequest = new StringRequest(titleUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();

                 titles = gson.fromJson(response, AllTitles.class);
                Message msg = new Message();
                msg.what=1;
                msg.obj=titles;
                handler.sendMessage(msg);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                handler.sendEmptyMessage(3);
            }
        });
        StringRequest topicRequest = new StringRequest(topicUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
            Gson gson = new Gson();
                AllDataBeans allTopic = gson.fromJson(response,AllDataBeans.class);
                Message msg = new Message();
                msg.what=2;
                msg.obj=allTopic;
                handler.sendMessage(msg);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        SingleMode.getInstance()._addRequest(titleRequest);
        SingleMode.getInstance()._addRequest(topicRequest);
    }
}
